package com.wangyb.wework.service.contact;

import com.wangyb.wework.entity.contact.WwContact;
import com.wangyb.wework.entity.contact.WwContactEmployee;
import com.wangyb.wework.entity.employee.WwEmployee;
import com.wangyb.wework.service.IWwEmployeeService;
import com.wangyb.wework.utils.WxApiUtils;
import com.wangyb.wework.wxApi.ExternalContactResp;
import com.wangyb.wework.wxApi.callback.WxCallbackMsg;
import org.redisson.api.RTopic;
import org.redisson.api.RedissonClient;
import org.redisson.api.listener.MessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

import static com.wangyb.wework.enums.WxCallbackEventEnum.EVENT_CONTACT;

/**
 * @Author: wangyb
 * @Date: 2021/8/8 1:18 上午
 */
@Component
public class WxContactCallbackService {
	@Autowired
	private IWwEmployeeService iEmployeeService;

	@Autowired
	private IWwTagService iContactTagService;
	@Autowired
	private IWwContactService iWwContactService;

	@Autowired
	private IExternalContactService iExternalContactService;
	@Autowired
	private IWwContactEmployeeService iContactEmployeeService;
	@Autowired
	private IWwContactTagPivotService iWwContactTagPivotService;
	@Autowired
	private RedissonClient redissonClient;
	@Autowired IWwContactTrackService iWwContactTrackService;

	/**
	 * 订阅客户相关事件，入添加客户，删除客户，编辑客户等
	 * 参考 https://work.weixin.qq.com/api/doc/90000/90135/92130
	 */
	@PostConstruct
	public void subscribe(){
		RTopic topic = redissonClient.getTopic(EVENT_CONTACT.name());
		topic.addListener(WxCallbackMsg.class, new MessageListener<WxCallbackMsg>() {
			@Override
			public void onMessage(CharSequence channel, WxCallbackMsg msg) {
				//添加客户
				if ("add_external_contact".equals(msg.getChangType())){
					onContactAdd(msg);
				}
				//编辑客户，成员修改外部联系人的备注、手机号或标签时，回调该事件
				else if ("edit_external_contact".equals(msg.getChangType())){
					onContactEdit(msg);
				}
				//外部联系人免验证添加成员事件
				else if("add_half_external_contact".equals(msg.getChangType())){
					onContactAddHalf(msg);
				}
				//删除企业客户事件
				else if("del_external_contact".equals(msg.getChangType())){
					onContactDel(msg);
				}
				//删除跟进成员事件，成员被外部联系人删除时，回调该事件
				else if("del_follow_user".equals(msg.getChangType())){
					onContactEmployeeDel(msg);
				}
				//客户接替失败事件
				else if("transfer_fail".equals(msg.getChangType())){
					onContactTransFail(msg);
				}
			}
		});
	}

	private void onContactTransFail(WxCallbackMsg msg) {

	}

	/**
	 * 被客户删除
	 * @param msg
	 */
	private void onContactEmployeeDel(WxCallbackMsg msg) {
		Long corpId = msg.getCorpId();
		String externalWxUserid = msg.getString("ExternalUserID");
		String employeeWxUserid = msg.getString("UserID");
		WwContact contact = iWwContactService.getContactByWxUserid(externalWxUserid);
		WwEmployee employee = iEmployeeService.getByWxUserid(employeeWxUserid);
		iWwContactService.delContactEmployeeByContact(corpId,contact,employee);
	}

	/**
	 * 删除客户
	 * @param msg
	 */
	private void onContactDel(WxCallbackMsg msg) {
		Long corpId = msg.getCorpId();
		String externalWxUserid = msg.getString("ExternalUserID");
		String employeeWxUserid = msg.getString("UserID");
		WwContact contact = iWwContactService.getContactByWxUserid(externalWxUserid);
		WwEmployee employee = iEmployeeService.getByWxUserid(employeeWxUserid);
		iWwContactService.delContactEmployeeByEmployee(corpId,contact,employee);
	}

	/**
	 * 外部联系人免验证添加成员事件,此时成员尚未确认添加对方为好友
	 * @param msg
	 */
	private void onContactAddHalf(WxCallbackMsg msg) {
		Long corpId = msg.getCorpId();
		String externalWxUserid = msg.getString("ExternalUserID");
		String employeeWxUserid = msg.getString("UserID");
		//TODO 需要发消息提醒员工确认添加对方为好友
	}

	private void onContactEdit(WxCallbackMsg msg) {
		Long corpId = msg.getCorpId();
		String externalWxUserid = msg.getString("ExternalUserID");
		String employeeWxUserid = msg.getString("UserID");
		ExternalContactResp resp = WxApiUtils.getExternalContactInfo(corpId, externalWxUserid);
		WwContact remoteContact = resp.getContact();
		WwContact localContact = iWwContactService.getContactByWxUserid(externalWxUserid);
		iWwContactService.updateContact(corpId,resp,localContact,employeeWxUserid);
	}

	private void onContactAdd(WxCallbackMsg msg) {
		Long corpId = msg.getCorpId();
		String externalWxUserid = msg.getString("ExternalUserID");
		String employeeWxUserid = msg.getString("UserID");
		ExternalContactResp resp = WxApiUtils.getExternalContactInfo(corpId, externalWxUserid);
		WwContact contact = iWwContactService.addContact(corpId, resp, employeeWxUserid);
		WwContactEmployee remoteContactEmployee = resp.getContactEmployee(employeeWxUserid);

		//TODO 发送欢迎语welcome msg
		String welcomeCode = msg.getString("WelcomeCode");
	}
}
